//
//  TVLiveAppDelegate.m
//  TVLive
//
//  Created by sid on 21/05/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "TVLiveAppDelegate.h"
#import "RootViewController.h"

NSString *kScalingModeKey	= @"scalingMode";
NSString *kControlModeKey	= @"controlMode";
NSString *kBackgroundColorKey	= @"backgroundColor";

@implementation TVLiveAppDelegate

@synthesize window;
@synthesize navigationController;
@synthesize moviePlayer;

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{       	
	[window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
	
	return YES;
}


- (void)applicationDidFinishLaunching:(UIApplication *)application {	

#ifdef __IPHONE_3_2
	[[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(moviePreloadDidFinish:) 
												 name:MPMoviePlayerLoadStateDidChangeNotification
											   object:nil];
#else
	[[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(moviePreloadDidFinish:) 
												 name:MPMoviePlayerContentPreloadDidFinishNotification 
											   object:nil];
#endif
 
	[[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(moviePlayBackDidFinish:) 
												 name:MPMoviePlayerPlaybackDidFinishNotification 
											   object:nil];
	 
	[[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(movieScalingModeDidChange:) 
												 name:MPMoviePlayerScalingModeDidChangeNotification 
											   object:nil];
}

- (void)applicationWillTerminate:(UIApplication *)application
{
	// Save data if appropriate
}


-(void)initAndPlayMovie:(NSURL *)movieURL
{
	MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];

	if (mp)
	{
		self.moviePlayer = mp;
		[mp release];
#ifdef __IPHONE_3_2
		self.moviePlayer.controlStyle = MPMovieControlModeDefault;
#else
		self.moviePlayer.movieControlMode = MPMovieControlModeDefault;		
#endif

#ifdef __IPHONE_3_2
		self.moviePlayer.backgroundView.backgroundColor = [UIColor clearColor];
#else
		self.moviePlayer.backgroundColor = [UIColor blackColor];
#endif

		[self.moviePlayer play];
	}
}

- (void) moviePreloadDidFinish:(NSNotification*)notification
{
	/* 
	 < add your code here >
	 
	 MPMoviePlayerController* moviePlayerObj=[notification object];
	 etc.
	 */
}

- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
    /*     
	 < add your code here >
	 
	 MPMoviePlayerController* moviePlayerObj=[notification object];
	 etc.
	 */
}

- (void) movieScalingModeDidChange:(NSNotification*)notification
{
    /* 
	 < add your code here >
	 
	 MPMoviePlayerController* moviePlayerObj=[notification object];
	 etc.
	 */
}


- (void)dealloc 
{
	
#ifdef __IPHONE_3_2

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerLoadStateDidChangeNotification
                                                  object:nil];
#else
	[[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerContentPreloadDidFinishNotification
                                                  object:nil];
#endif
	
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerScalingModeDidChangeNotification
                                                  object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:OverlayViewTouchNotification
                                                  object:nil];
	[moviePlayer release]; 
	[navigationController release];
	[window release];
	[super dealloc];
}

@end